package com.mall.service.impl;

import com.mall.common.exception.ServiceException;
import com.mall.common.pojo.PageObject;
import com.mall.dao.RoleDao;
import com.mall.dao.RoleMenuDao;
import com.mall.dao.UserRoleDao;
import com.mall.pojo.Role;
import com.mall.pojo.RoleMenuVo;
import com.mall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleMenuDao roleMenuDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public PageObject<Role> findPageObjects(
            String name, Integer pageCurrent) {
        //1.参数校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("页码值不正确");
        //2.查询总记录数并校验
        int rowCount=roleDao.getRowCount(name);
        if(rowCount==0)
            throw new ServiceException("没有找到对应记录");
        //3.查询当前页记录
        int pageSize=2;
        int startIndex=(pageCurrent-1)*pageSize;
        List<Role> records=roleDao.findPageObjects(name,startIndex,pageSize);
        //4.封装查询结果并返回
//        PageObject pageObject=new PageObject<>();
//        pageObject.setRowCount(rowCount);
//        pageObject.setRecords(records);
//        pageObject.setPageSize(pageSize);
//        pageObject.setPageCurrent(pageCurrent);
//        int pageCount=rowCount/pageSize;
//        if(rowCount%pageSize!=0)pageCount++;
//        pageObject.setPageCount(pageCount);
//        return pageObject;

        return new PageObject<>(rowCount,records,pageSize,pageCurrent);

    }


    @Override
    public int deleteObject(Integer id) {
        if (id==null||id<=0) throw new IllegalArgumentException("请先选择");
        roleMenuDao.deleteObjectsByRoleId(id);
        userRoleDao.deleteObjectsByRoleId(id);
        int rows = roleDao.deleteObject(id);
        if (rows==0) throw new ServiceException("此记录可能已经不存在");
        return rows;
    }

    @Transactional//此注解描述的方法为事务切入点方法，方法执行之前会开启事务，执行结束会提交或回滚事务
    @Override
    public int saveObject(Role entity,Integer[] menuIds){
        if (entity==null) throw new IllegalArgumentException("保存对象不能为空");
        if (StringUtils.isEmpty(entity.getName())) throw new IllegalArgumentException("角色名不能为空");
        if (menuIds==null||menuIds.length==0) throw new ServiceException("必须为角色分配权限");
        int rows = roleDao.insertObject(entity);
        roleMenuDao.insertObjects(entity.getId(),menuIds);
        return rows;
    }

    @Override
    public RoleMenuVo findObjectById(Integer id){
        if (id==null||id<0) throw new IllegalArgumentException("id的值不合法");
        RoleMenuVo result = roleDao.findObjectById(id);
        if (result==null) throw new ServiceException("此记录已经不存在");
        return result;
    }

    @Transactional//此注解描述的方法为事务切入点方法，执行之前切入开启事务，执行结束提交或回滚事务
    @Override
    public int updateObject(Role entity,Integer[] menuIds){
        if (entity==null) throw new IllegalArgumentException("更新的对象不能为空");
        if (entity.getId()==null) throw new IllegalArgumentException("id的值不能为空");
        if (StringUtils.isEmpty(entity.getName())) throw new IllegalArgumentException("角色名不能为空");
        if (menuIds==null||menuIds.length==0) throw new IllegalArgumentException("必须为角色指定一个权限");
        int rows = roleDao.updateObject(entity);
        if (rows==0) throw new ServiceException("对象可能已经不存在");
        roleMenuDao.deleteObjectsByRoleId(entity.getId());
        roleMenuDao.insertObjects(entity.getId(),menuIds);
        return rows;
    }

}
